package ui.view;

import joinery.DataFrame;
import org.jfree.ui.tabbedui.VerticalLayout;
import tool.Lg;
import ui.listener.HTableMode;

import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.Map;

public class HScrollTableView extends JPanel {
    private HTable hTable;
    private HTableMode abstractTableModel;
    private JScrollPane scrollPane = new JScrollPane();

    public HScrollTableView()
    {
        this.setLayout(new GridLayout(1,1));
//        this.setLayout(new VerticalLayout());
        this.setBackground(Color.white);
        abstractTableModel = new HTableMode();
        hTable = new HTable(abstractTableModel);

        scrollPane.setViewportView(hTable);

        add(scrollPane);
    }

    public void reload(DataFrame<String> dataFrame)
    {
        abstractTableModel.loadDataframe(dataFrame);
    }

    public HTableMode getModel() {
        return abstractTableModel;
    }

    public void reload(DataFrame<String> dataFrame, String...strings)
    {
        abstractTableModel.loadDataframe(dataFrame,strings);
//        resizeTable(false);

        Dimension tableSize = hTable.getPreferredSize();
        Dimension preferredSize = scrollPane.getPreferredSize();
        Point scrollPaneLocation = scrollPane.getLocation();
//        Lg.info(scrollPaneLocation);

        Dimension thisSize = getPreferredSize();

        preferredSize.setSize(tableSize.width,thisSize.height);
        scrollPane.setPreferredSize(preferredSize);
    }
    public void dropCol(String...strings)
    {
        abstractTableModel.dropCol(strings);
    }
    public Object getRealValueAt(int rowIndex, int columnIndex)
    {
        return abstractTableModel.getRealValueAt(rowIndex,columnIndex);
    }
    public HTable gethTable() {
        return hTable;
    }

    public Map<Integer, List<String>> deleteSelects()
    {
        int[] selectedRows = hTable.getSelectedRows();
        return abstractTableModel.deleteRows(selectedRows);
    }

    public void resizeTable(boolean bool) {
        Dimension containerwidth = null;
        if (!bool) {
            //初始化时，父容器大小为首选大小，实际大小为0
            containerwidth = scrollPane.getPreferredSize();
        } else {
            //界面显示后，如果父容器大小改变，使用实际大小而不是首选大小
            containerwidth = scrollPane.getSize();
        }
        //计算表格总体宽度
        int allwidth = hTable.getIntercellSpacing().width;
        for (int j = 0; j < hTable.getColumnCount(); j++) {
            //计算该列中最长的宽度
            int max = 0;
            for (int i = 0; i < hTable.getRowCount(); i++) {
                int width = hTable.
                        getCellRenderer(i, j).getTableCellRendererComponent
                        (hTable, hTable.getValueAt(i, j), false,
                                false, i, j).getPreferredSize().width;
                if (width > max) {
                    max = width;
                }
            }
            //计算表头的宽度
            int headerwidth = hTable.
                    getTableHeader().
                    getDefaultRenderer().getTableCellRendererComponent(
                    hTable, hTable.getColumnModel().
                            getColumn(j).getIdentifier(), false, false,
                    -1, j).getPreferredSize().width;
            //列宽至少应为列头宽度
            max += headerwidth;
            //设置列宽
            hTable.getColumnModel().
                    getColumn(j).setPreferredWidth(max);
            //给表格的整体宽度赋值，记得要加上单元格之间的线条宽度1个像素
            allwidth += max + hTable.getIntercellSpacing().width;
        }
        allwidth += hTable.getIntercellSpacing().width;
        //如果表格实际宽度大小父容器的宽度，则需要我们手动适应；否则让表格自适应
        if (allwidth > containerwidth.width) {
            hTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        } else {
            hTable.
                    setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
        }
    }

}


